$(function () {
       	    queryAllData();
       	});
        function recovery(){
        	$("#searchText1").val("");
        	refresh($("#mainData"));
        }
        function edit(index) {
            // 获取数据
            $("#mainData").bootstrapTable('uncheckAll');
            $("#mainData").bootstrapTable('check', index);
            var node = $('#mainData').bootstrapTable("getSelections");
            var cou_id = node[node.length-1].id;
            var cou_name = node[node.length-1].name;
            var cou_sortFlag = node[node.length-1].sortFlag;
            
            // 清空弹窗的各个输入框
            cleanUpInModal($("#editModal"));
            // 赋值
            $("#edit_cou_id").val(cou_id);
            $("#edit_cou_name").val(cou_name);
            $("#edit_cou_sortFlag").val(cou_sortFlag);
            // 打开弹窗
            $("#editModal").modal("show");
            refresh($("#mainData"));
        }
        function editInvInfo(){
        	var id = $("#edit_cou_id").val();
        	var name =  $("#edit_cou_name").val();
        	var sortFlag = $("#edit_cou_sortFlag").val();
        	var param = {};
        	param.id = id;
        	param.name = name;
        	param.sortFlag = sortFlag;
        	if(name!=''&&sortFlag!=''){
        		$.ajax({
            		url:"/cgc/editData",
            		type:"post",
            		//dataType:"json",
            		data:param,
            		async:true,
            		complete:function(msg){
            			var data = eval("(" + msg.responseText + ")");
            			if(data["flag"]>0){
            				swal({
                                title: "提示",
                                text: "修改成功",
                                type: "success"
                            })
            			}else{
            				swal({
                                title: "提示",
                                text: "修改失败",
                                type: "warning",
                                showCancelButton: false,
                                confirmButtonColor: "#DD6B55",
                                confirmButtonText: "确定",
                                closeOnConfirm: true
                            });
            			}
            			refresh($("#mainData"));
            			$("body").modal("hide");
            			$("#editModal").modal("hide");
            		}
            	})
        	}else{
        		swal({
                    title: "提示",
                    text: "*为必填项",
                    type: "warning",
                    showCancelButton: false,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确定",
                    closeOnConfirm: true
                });
        	}
        	
        }
        // 打开弹窗
        function addWindow() {
            $("#addModal").modal("show");
        }
      //添加提交
        function addSubmit() {
        	var name = $("#add_cou_name").val();
        	var sortFlag = $("#add_cou_sortFlag").val();
        	var param = {};
        	param.name = name;
        	param.sortFlag = sortFlag;
        	if(name!=''&&sortFlag!=''){
        		$.ajax({
            		url:"/cgc/insertData",
            		type:"post",
            		//dataType:"json",
            		data:param,
            		async:true,
            		complete:function(msg){
            			var data = eval("(" + msg.responseText + ")");
            			if(data["flag"]>0){
            				swal({
                                title: "提示",
                                text: "添加成功",
                                type: "success"
                            })
                            $("body").mLoading("hide");
            			}else{
            				swal({
                                title: "提示",
                                text: "添加失败",
                                type: "warning",
                                showCancelButton: false,
                                confirmButtonColor: "#DD6B55",
                                confirmButtonText: "确定",
                                closeOnConfirm: true
                            });
            				$("body").mLoading("hide");
            			}
            			refresh($("#mainData"));
            			$("body").modal("hide");
            			$("#addModal").modal("hide");
            		}
            	})
        	}else{
        		swal({
                    title: "提示",
                    text: "*为必填项",
                    type: "warning",
                    showCancelButton: false,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确定",
                    closeOnConfirm: true
                });
				$("body").mLoading("hide");
        	}
		}
        function deleteData(value){
            swal({
    	        title: "您确定要删除这条信息吗",
    	        text: "删除后将无法恢复，请谨慎操作！",
    	        type: "warning",
    	        showCancelButton: true,
    	        confirmButtonColor: "#DD6B55",
    	        confirmButtonText: "确定",
    	        closeOnConfirm: false
    	      },function(){
	            var param = {};
	            param.id = value;
	            refresh($("#mainData"));
	            $.ajax({
	        		url:"/cgc/deleteData",
	        		type:"post",
	        		//dataType:"json",
	        		data:param,
	        		async:true,
	        		complete:function(msg){
	        			var data = eval("(" + msg.responseText + ")");
	        			if(data["flag"]>0){
	        				swal({
	                            title: "提示",
	                            text: "删除成功",
	                            type: "success"
	                        })
	                        $("body").mLoading("hide");
	        			}else{
	        				swal({
	                            title: "提示",
	                            text: "删除失败",
	                            type: "warning",
	                            showCancelButton: false,
	                            confirmButtonColor: "#DD6B55",
	                            confirmButtonText: "确定",
	                            closeOnConfirm: true
	                        });
	        				$("body").mLoading("hide");
	        			}
	        			refresh($("#mainData"));
	        			$("body").modal("hide");
	        			//$("#editModal").modal("hide");
	        		}
	        	})
    	     })
        }
        function queryData(){
        	var mess = $("#searchText1").val();
        	$("#mainData").bootstrapTable('refresh', {url:'/cgc/getData',
                query: {mess: mess}});
        }
    	function queryAllData(){
    		var url = "/cgc/getAllData";
    	    var cArray=[[
    	        {field:'name',title:'商品分类', width : '50',align: 'center', valign: 'middle'},
    	        {field:'sortFlag',title:'排序', width : '50',align: 'center', valign: 'middle'},
    	        {
    	            field: 'id',
    	            title: '操作',
    	            align: 'center',
    	            valign: 'middle',width : '50',
    	            formatter: function (value, row, index) {
    	                return "<button class='btn btn-primary btn-xs' type='button'  onclick='edit(" + index + ")'><i class='fa fa-edit'></i>&nbsp;修改</button>"+
    	                "&nbsp;&nbsp;<button class='btn btn-danger btn-xs' type='button'  onclick='deleteData(" + value + ")'><i class='fa fa-edit'></i>&nbsp;删除</button>";
    	            }
    	        }
    	    ]];
    	    loadTable( $("#mainData"), url, cArray, $("#table_toolbar"));

    	}